﻿/*********************************************************
 * copyright learnren.com 版权所有
 * 开发人员：minyuan
 * 创建时间：2013/6/30 17:36:44
 * 描述说明：
 * 
 * 更改历史：
 * 
 * *******************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LEARNREN.COM.TOOL;
using LEARNREN.COM.Entity;
using LEARNREN.COM.BLL;
using LEARNREN.COM.UI.Template;
using LEARNREN.COM.BLL.Enums;
using System.Web.UI;

namespace LEARNREN.COM.UI.LabelProviders
{
    /// <summary>
    /// 案例列表标签
    /// </summary>
    class FLLLabel : ListLabelBase
    {
        public FLLLabel(string label)
            : base(label)
        {

        }

        public override string GetHtml()
        {
            try
            {
                string[] paras = LabelProviderHelper.GetProperties(this.LabelString);

                if (paras.Length < 3)
                {
                    return this.LabelString;
                }

                string countToGet = paras[0];
                string showType = paras[1];
                // 是否用默认内容补齐，0不补齐，1补齐
                string fillWithDefault = paras[2];

                int countToGetInt = 0;
                int.TryParse(countToGet, out countToGetInt);

                int total;

                // 符合条件的记录
                List<FriendLink> links = null;

                if (countToGetInt == 0)
                {
                    links = this.GroupLogicFactory.FriendLinkLogic.GetListASC(p => p.Order);
                    total = links.Count;
                }
                else
                {
                    links = this.GroupLogicFactory.FriendLinkLogic.GetListByPage(out total,
                                                                                 p => true,
                                                                                 p => p.CreatedTime,
                                                                                 OrderEnum.Descending,
                                                                                 countToGetInt,
                                                                                 1);
                }

                // 组装
                StringBuilder sb = new StringBuilder();
                using (System.IO.StringWriter wr = new System.IO.StringWriter(sb))
                {
                    using (System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(wr))
                    {
                        htmlWriter.AddAttribute(HtmlTextWriterAttribute.Class, "friendLink");
                        htmlWriter.BeginRender();
                        htmlWriter.RenderBeginTag(HtmlTextWriterTag.Ul);

                        foreach (var link in links)
                        {
                            htmlWriter.RenderBeginTag(HtmlTextWriterTag.Li);
                            htmlWriter.AddAttribute(HtmlTextWriterAttribute.Href, link.URL);
                            htmlWriter.RenderBeginTag(HtmlTextWriterTag.A);
                            if (showType == "0")
                            {
                                //0文字1图片
                                htmlWriter.Write(link.Title);
                            }
                            else
                            {
                                htmlWriter.AddAttribute(HtmlTextWriterAttribute.Src, link.Image);
                                htmlWriter.AddAttribute(HtmlTextWriterAttribute.Alt, link.Title);
                                //htmlWriter.AddAttribute(HtmlTextWriterAttribute.Width, link.ImageWidth.ToString());
                                //htmlWriter.AddAttribute(HtmlTextWriterAttribute.Height, link.ImageHeight.ToString());
                                htmlWriter.RenderBeginTag(HtmlTextWriterTag.Img);
                                htmlWriter.RenderEndTag();
                            }
                            htmlWriter.RenderEndTag();
                            htmlWriter.RenderEndTag();
                        }

                        if (countToGetInt != 0 && fillWithDefault != "0" && total < countToGetInt)
                        {
                            //需要补齐的情况
                            for (int i = 0; i < countToGetInt - total; i++)
                            {
                                htmlWriter.RenderBeginTag(HtmlTextWriterTag.Li);
                                htmlWriter.AddAttribute(HtmlTextWriterAttribute.Href, BasicCache.SiteConfig.SiteUrl);
                                htmlWriter.RenderBeginTag(HtmlTextWriterTag.A);
                                if (showType == "0")
                                {
                                    //0文字1图片
                                    htmlWriter.Write(BasicCache.SiteConfig.SiteName);
                                }
                                else
                                {
                                    htmlWriter.AddAttribute(HtmlTextWriterAttribute.Src, System.Web.VirtualPathUtility.ToAbsolute("~/images/friendlinkempty.jpg"));
                                    htmlWriter.AddAttribute(HtmlTextWriterAttribute.Alt, BasicCache.SiteConfig.SiteName);
                                    //htmlWriter.AddAttribute(HtmlTextWriterAttribute.Width, link.ImageWidth.ToString());
                                    //htmlWriter.AddAttribute(HtmlTextWriterAttribute.Height, link.ImageHeight.ToString());
                                    htmlWriter.RenderBeginTag(HtmlTextWriterTag.Img);
                                    htmlWriter.RenderEndTag();
                                }
                                htmlWriter.RenderEndTag();
                                htmlWriter.RenderEndTag();
                            }
                        }

                        htmlWriter.RenderEndTag();
                        wr.Flush();
                        htmlWriter.EndRender();
                        return sb.ToString();
                    }
                }
            }
            catch
            {
                return this.LabelString;
            }
        }

    }
}
